import { useAccessStore } from '@vben/stores';

import { client } from './client';

export namespace AuthApi {
  /** 登录接口参数 */
  export interface LoginParams {
    password?: string;
    username?: string;
  }

  /** 登录接口返回值 */
  export interface LoginResult {
    accessToken: string;
    refreshToken?: string;
  }

  /** 注册接口参数 */
  export interface RegisterParams {
    username: string;
    email: string;
    password: string;
    displayName?: string;
  }

  /** 注册接口返回值 */
  export interface RegisterResult {
    success: boolean;
    message?: string;
  }

  export interface RefreshTokenResult {
    data: string;
    status: number;
  }
}

/**
 * 登录
 */
export async function loginApi(data: AuthApi.LoginParams) {
  // return requestClient.post<AuthApi.LoginResult>('/auth/login', data);
  if (!data.username || !data.password) {
    throw new Error('用户名和密码不能为空');
  }
  const { data: result } = await client.user.login({
    password: data.password,
    usernameOrEmail: data.username,
  });

  if (result.success) {
    return {
      accessToken: result.data?.token,
      refreshToken: result.data?.refreshToken,
    } as AuthApi.LoginResult;
  } else {
    throw new Error(result.message || '登录失败');
  }
}

/**
 * 注册
 */
export async function registerApi(data: AuthApi.RegisterParams) {
  if (!data.username || !data.email || !data.password) {
    throw new Error('用户名、邮箱和密码不能为空');
  }

  const { data: result } = await client.user.register({
    username: data.username,
    email: data.email,
    password: data.password,
    displayName: data.displayName,
  });

  if (result.success) {
    return {
      success: true,
      message: result.message || '注册成功',
    } as AuthApi.RegisterResult;
  } else {
    throw new Error(result.message || '注册失败');
  }
}

/**
 * 刷新accessToken
 */
export async function refreshTokenApi() {
  // return baseRequestClient.post<AuthApi.RefreshTokenResult>('/auth/refresh', {
  //   withCredentials: true,
  // });
  const store = useAccessStore();
  if (!store.refreshToken) {
    throw new Error('没有可用的刷新令牌');
  }
  const { data: result } = await client.user.refreshToken({
    refreshToken: store.refreshToken,
  });
  if (result.success) {
    return {
      data: result.data?.token,
      status: 200,
    } as AuthApi.RefreshTokenResult;
  } else {
    throw new Error(result.message || '刷新令牌失败');
  }
}

/**
 * 退出登录
 */
export async function logoutApi() {
  // return baseRequestClient.post('/auth/logout', {
  //   withCredentials: true,
  // });
  const store = useAccessStore();
  if (!store.accessToken) {
    throw new Error('没有可用的访问令牌');
  }
  if (store.refreshToken) {
    const { data: result } = await client.user.logout({
      refreshToken: store.refreshToken,
    });
    if (!result.success) throw new Error(result.message || '退出登录失败');
  }
  store.setAccessToken(null);
  store.setRefreshToken(null);
}

/**
 * 获取用户权限码
 */
export async function getAccessCodesApi() {
  // return requestClient.get<string[]>('/auth/codes');
  // const { data: result } = await client.user.getAccessCodes();
  // if (result.success) {
  //   return result.data || [];
  // }
  // throw new Error(result.message || '获取权限码失败');
  // throw new Error('获取权限码接口未实现');
  return [];
}
